$(document).ready(function(){
	
	$('button[id^=payBtn]').click(function(){
		var id = this.id.split('-')[1];
		confirmMsg('确定支付订单吗？',function(){
			doPost(getContextPath()+'/order/pay/'+id, null, function(data){
				if (data.errorCode == '0') {
					showMsg('支付订单成功。',true,function(){
						window.location.reload();
					});
				}else if (data.errorCode == '3001') {
					showMsg('支付订单失败，余额不足。',false);
				} else {
					showMsg('支付订单失败，请重新操作。',false);
				}
			});
		});
	});
	
	$('button[id^=recBtn]').click(function(){
		var id = this.id.split('-')[1];
		confirmMsg('确定已收到货吗？',function(){
			doPost(getContextPath()+'/order/received/'+id, null, function(){
				window.location.reload();
			});
		});
	});
	
	$('button[id^=subCommentsBtn]').click(function(){
		var id = this.id.split('-')[1];
		doPost(getContextPath()+'/comments/submit', $('form:first').serialize(), function(data){
			if (data.errorCode == '0') {
				showMsg('评价成功。',true,function(){
					
					if (id.indexOf('O') == 0) {
						window.location.href = getContextPath()+'/order/list';
					} else if (id.indexOf('S') == 0) {
						window.location.href = getContextPath()+'/trade/soldlist';
					} else if (id.indexOf('E') == 0) {
						window.location.href = getContextPath()+'/trade/exchangelist';
					}
				});
			} else {
				showMsg('评价失败，请重新操作。',false);
			}
		});
	});
	
	$('button[id^=soldBookBtn-]').click(function(){
		var id = this.id.split('-')[1];
		$('.soldBook-modal #name').text($('#name-'+id).val());
		$('.soldBook-modal #price').text($('#price-'+id).val());
		$('.soldBook-modal #num').text($('#num-'+id).val());
		$('.soldBook-modal #total').text($('#total-'+id).val());
		$('.soldBook-modal #soldTotal').val($('#total-'+id).val());
		$('.soldBook-modal #soldTotal').val($('#total-'+id).val());
		$('.soldBook-modal #orderItemId').val(id);
	});
	
	$('#soldBookBtn').click(function(){
		doPost(getContextPath()+'/trade/sold', $('#soldBookForm').serialize(), function(){
			window.location.reload();
		});
	});
	
	$('button[id^=exchBookBtn-]').click(function(){
		var id = this.id.split('-')[1];
		$('.exchBook-modal #name').text($('#name-'+id).val());
		$('.exchBook-modal #price').text($('#price-'+id).val());
		$('.exchBook-modal #num').text($('#num-'+id).val());
		$('.exchBook-modal #total').text($('#total-'+id).val());
		$('.exchBook-modal #orderItemId').val(id);
	});
	
	$('#exchBookBtn').click(function(){
		doPost(getContextPath()+'/trade/exchange', $('#exchBookForm').serialize(), function(){
			window.location.reload();
		});
	});
	
	
	$('button[id^=deliveryBtn-]').click(function(){
		var id = this.id.split('-')[1];
		confirmMsg('确定已发货吗？',function(){
			doPost(getContextPath()+'/trade/delivery/'+id, null, function(){
				window.location.reload();
			});
		});
	});
	$('button[id^=receivedBtn-]').click(function(){
		var id = this.id.split('-')[1];
		confirmMsg('确定已收到货吗？',function(){
			doPost(getContextPath()+'/trade/received/'+id, null, function(){
				window.location.reload();
			});
		});
	});
	$('button[id^=closeBtn-]').click(function(){
		var id = this.id.split('-')[1];
		confirmMsg('确定关闭交易吗？',function(){
			doPost(getContextPath()+'/trade/close/'+id, null, function(){
				window.location.reload();
			});
		});
	});
	
});